//
//  Tools.m
//  easymarketing
//
//  Created by HailongHan on 14/12/22.
//  Copyright (c) 2014年 cubead. All rights reserved.
//

#import "Tools.h"

@implementation Tools

+(NSString *)base64String:(NSString *)str{
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    return [NSString base64StringFromData:data length:data.length];
}

+(void)animateWithRotate:(UIView *)view duration:(int)duration repeatCount:(int)repeatCount{
    //图片开始旋转
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    rotationAnimation.duration = duration;
    rotationAnimation.repeatCount = repeatCount;//你可以设置到最大的整数值
    rotationAnimation.cumulative = NO;
    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;
    [view.layer addAnimation:rotationAnimation forKey:@"Rotation"];
}

+(CGSize)sizeOfSingleLineText:(NSString *)content withSize:(CGFloat)textSize{
    if (content == nil) {
        return CGSizeZero;
    }
    CGSize size = [content sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:textSize]}];
    return size;
}

+(CGSize)sizeOfSingleLineBoldText:(NSString *)content withSize:(CGFloat)textSize{
    if ([self isEmpty:content]) {
        return CGSizeZero;
    }
    CGSize size = [content sizeWithAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:textSize]}];
    return size;
}

+(CGSize)sizeOfMultiLineText:(NSString *)content withSize:(CGFloat)textSize withWidth:(CGFloat)with{
    if (content == nil) {
        return CGSizeZero;
    }
    
    CGSize size = [content boundingRectWithSize:CGSizeMake(with, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:textSize]} context:nil].size;
    return size;
}

+(void)showMessageCenter:(NSString *)msg{
    [kWindow makeToast:msg duration:3.0 position:CSToastPositionCenter];
}

+(void)showMessageBottom:(NSString *)msg{
    [kWindow makeToast:msg];
}

+(void)saveKeyValues:(id)value forKey:(NSString *)key{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:value forKey:key];
    [userDefaults synchronize];
}

+(id)getValueForKey:(NSString *)key{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults objectForKey:key];
}

+ (void)printJsonStr:(id)data {
    NSData *jsonData = [data JSONString];
    NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"json data:%@",json);
}

+ (void)saveApplicationData:(id)objectData forKey: (NSString*)key{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:objectData forKey:key];
    [ud synchronize];
}

+ (id)getApplicationData:(NSString*)key{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    return [ud objectForKey:key];
}

+ (void)removeApplicationDataByKey:(NSString *)key{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud removeObjectForKey:key];
    [ud synchronize];
}

+ (BOOL)validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

+(BOOL)isEmpty:(NSString *)string{
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

+ (NSString*)dictionaryToJson:(NSDictionary *)dic {
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
    
}

+ (NSString *)getExpireDateWithHour:(float) hour {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strDate = [dateFormatter stringFromDate:[[NSDate date] dateByAddingHours:hour]];
    return strDate;
}

+ (NSString*)getResourceFullPath:(NSString*)path {
    NSString *resource = [NSString stringWithFormat:@"http://120.78.71.48/outes/%@", path];
    return resource;
}

+ (NSURL*)getResourceFullPathURL:(NSString*)path {
    NSString *picUrl = [NSString stringWithFormat:@"http://120.78.71.48/outes/%@", path];
    NSString* webStringURL = [picUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    return [NSURL URLWithString:webStringURL];
}

//手机号码验证
+ (BOOL)validateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

@end
